//创建小角色相关小仓库
import { defineStore } from "pinia";
//引入接口
import { addRole, getRoleList, updateRole} from "@/api/role";
//引入数据类型
import type { RoleState } from "./types/roleType";
//引数据类型
import type {addRoleResponseData, getRoleListResponseData, roleInfoData } from "@/api/role/type";
//创建用户小仓库
let useRoleStore = defineStore("Role",{
    state:():RoleState=>{
        return{
            roles:[]
        }
    },
    actions:{
        async roleList(){
            let result:getRoleListResponseData=await getRoleList();
            if(result.status == 0){
                this.roles=result.data
                return "OK"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        //添加角色
        async useAddRole(data:roleInfoData){
            let result:addRoleResponseData=await addRole(data);
            if(result.status==0){
                this.roleList()
                return "OK"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        //设置角色权限
        async updateRole(data:roleInfoData){
            let result:addRoleResponseData=await updateRole(data)
            if(result.status == 0){
                this.roleList()
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        }
    },
    getters:{

    }
})
export default useRoleStore


